package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.StorageRecord;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.StorageRecordMapper;
import com.ktjiaoyu.service.StorageRecordService;
import com.ktjiaoyu.utils.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("StorageRecordService")
public class StorageRecordServiceImpl implements StorageRecordService {
    private static final Logger LOGGER= LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private StorageRecordMapper storageRecordMapper;

    @Override
    public int saveBatch(List<StorageRecord> storageRecordList) {
        return 0;
    }

    @Override
    public Page<StorageRecord> findPage(StorageRecord entity, Integer pageNo, Integer pageSize) throws Exception {
        Page<StorageRecord> page=null;
        try{
            page=new Page<StorageRecord>();
            page.setPageSize(pageSize);
            Long count= storageRecordMapper.count(entity);
            page.setTotalCount(count.intValue());
            page.setPageNo(pageNo);

            List<StorageRecord> data=storageRecordMapper.getPage(entity,page.offset(),page.getPageSize());

            page.setData(data);
        }catch (Exception e){
            LOGGER.error("StorageRecordServiceImpl.findPage(StorageRecord.Integer,Integer)方法出现异常。",e);
            throw e;
        }
        return page;
    }

    @Override
    public boolean saveOrUpdate(StorageRecord entity) throws Exception {
        try {
            if (entity.getId()==null){
                return storageRecordMapper.insert(entity)>0;
            }else {
                return storageRecordMapper.update(entity)>0;
            }
        }catch (Exception e){
            LOGGER.error("执行StorageRecordServiceImpl.saveOrUpdate(StorageRecord)方法出现异常。",e);
            throw e;
        }
    }

    @Override
    public StorageRecord findById(Long id) throws Exception {
        try {
            return storageRecordMapper.getById(id);
        }catch (Exception e){
            LOGGER.error("执行StorageRecordServiceImpl.findById(Long)方法出现异常。",e);
            throw e;
        }
    }

    @Override
    public StorageRecord findBySrCode(String srCode) throws Exception {
        try {
            return storageRecordMapper.getBySrCode(srCode);
        }catch (Exception e){
            LOGGER.error("执行StorageRecordServiceImpl.findBySrCode(String)方法出现异常。",e);
            throw e;
        }
    }

    @Override
    public int deleteById(Long id) throws Exception {
        try {
            return storageRecordMapper.deleteById(id);
        }catch (Exception e){
            LOGGER.error("执行StorageRecordServiceImpl.deleteById(Long)方法出现异常。",e);
            throw e;
        }
    }
}
